package com.fruit.redis.conf;

import com.joe.utils.cluster.ClusterManager;
import com.joe.utils.cluster.redis.RedisBaseConfig;
import com.joe.utils.cluster.redis.RedisSingleServerConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redis配置
 *
 * @author joe
 * @version 2018.04.24 17:06
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({RedisConf.class})
public class RedisBeanConf {
    @Autowired
    private RedisConf redisConf;

    @Bean
    public RedisBaseConfig redisBaseConfig() {
        RedisSingleServerConfig config = new RedisSingleServerConfig();
        config.setAddress(redisConf.getHost() + ":" + redisConf.getPort());
        config.setTimeout(redisConf.getTimeout());
        config.setPassword(redisConf.getPassword());
        config.setConnectionPoolSize(redisConf.getMaxIdle());
        config.setConnectionMinimumIdleSize(redisConf.getMinIdle());
        return config;
    }

    @Bean
    public ClusterManager clusterManager(RedisBaseConfig redisBaseConfig) {
        ClusterManager clusterManager = null;
        try {
            log.debug("初始化锁服务，redis配置是：[{}]", redisBaseConfig);
            clusterManager = ClusterManager.getInstance(redisBaseConfig);
            log.debug("初始化的分布式管理器为：{}", redisBaseConfig);
        } catch (Exception e) {
            log.warn("锁服务初始化失败", e);
        }
        return clusterManager;
    }

}
